﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FinanSoft.Entities;
using FinanSoft.BusinessLayer;
using FinanSoft.Utilities;
using FinanSoft.PresentacionSociosNegocios;
using System.Xml;
using System.Data.SqlClient;

namespace FinanSoft.PresentacionSociosNegocios
{
    public partial class frmPreviewDocs : Form
    {
        String tipoDoc; // 1: FP, 2:FC, 3:NC
        Document docMostrar;

        public frmPreviewDocs(Document _docMostrar)
        {
            docMostrar = _docMostrar;
            tipoDoc = docMostrar.tipoDocumento;

            InitializeComponent();

            //Título del documento
            if (tipoDoc == "FP")
            {
                lblTipoDoc.Text = "Factura de Proveedor"; 
                llbDirigidoA.Text = "Proveedor";
            }
            else if (tipoDoc == "FC")
            {
                lblTipoDoc.Text = "Factura de Cliente";  
                llbDirigidoA.Text = "Cliente";
            }
            else if (tipoDoc == "NC")
            {
                lblTipoDoc.Text = "Nota de Crédito"; 
                llbDirigidoA.Text = "Cliente";
            }

            //Encabezado del documento
            cmbOwner.Items.Insert(0, docMostrar.owner);
            cmbOwner.SelectedIndex = 0;

            dtpFechaIni.Value = docMostrar.fechaEmision;
            dtpFechaFin.Value = docMostrar.fechaVencimiento;

            txtNumero.Text = System.Convert.ToString(docMostrar.codigoDocumento);

            cmbCuenta.Items.Insert(0, docMostrar.cuenta);
            cmbCuenta.SelectedIndex = 0;
            
            //Detalle del documento
            gridDetalle.Rows.Clear();
            gridDetalle.Refresh();

            if (docMostrar.detalleDoc.Length > 0)
            {
                for (int elem = 0; elem < docMostrar.detalleDoc.Length; elem++)
                {
                    gridDetalle.Rows.Add();
                    gridDetalle[0, elem].Value = docMostrar.detalleDoc[elem].Item.CodArticulo;
                    gridDetalle[1, elem].Value = docMostrar.detalleDoc[elem].Item.Descripción;
                    gridDetalle[2, elem].Value = ItemManager.GetInstance().GetMeasurementUnit(docMostrar.detalleDoc[elem].Item.UnidadMedida).acronimo;
                    gridDetalle[3, elem].Value = docMostrar.detalleDoc[elem].Quantity;
                    gridDetalle[4, elem].Value = docMostrar.detalleDoc[elem].StoreRoom.Name;
                    gridDetalle[5, elem].Value = MoneyManager.GetInstance().GetCurrency(docMostrar.IdMoneda).Symbol + " " + docMostrar.detalleDoc[elem].UnitCost.ToString("0.00");
                    gridDetalle[6, elem].Value = docMostrar.detalleDoc[elem].Tax.Nombre;
                }
            }
            else
            {
                List<DocService> services = DocumentManager.GetInstance().GetDocumentServices(docMostrar);
                int elem = 0;
                foreach (DocService service in services)
                {
                    gridDetalle.Rows.Add();
                    gridDetalle[0, elem].Value = service.SalesAccount.AccountCode;
                    gridDetalle[1, elem].Value = service.Description;
                    gridDetalle[2, elem].Value = "-";
                    gridDetalle[3, elem].Value = "-";
                    gridDetalle[4, elem].Value = "-";
                    gridDetalle[5, elem].Value = service.ServiceCurrency.Symbol + " " + service.CostAmount.ToString("0.00");
                    gridDetalle[6, elem].Value = service.ServiceTax.Nombre;
                    elem++;
                }
            }

            //Totales
            Currency docCurrency = MoneyManager.GetInstance().GetCurrency(docMostrar.IdMoneda);
            txtSubtotal.Text = docCurrency.Symbol +" " + docMostrar.subTotal.ToString("0.00");
            txtImp.Text = docCurrency.Symbol + " " + docMostrar.impuestos.ToString("0.00");
            txtDescuentos.Text = docCurrency.Symbol + " 0.00";
            txtTotal.Text = docCurrency.Symbol + " " + docMostrar.total.ToString("0.00"); 

        }

        private void btnSalir_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
